// govSuperviseTourist/pages/applyAppeal/applyAppeal.js
const WXAPI = require('../../../api/govSuperviseTourist');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1, // 是否显示左上角图标   1表示显示    0表示不显示
      title: '咨询投诉', // 导航栏 中间的标题
      textColor: '#000', // 标题颜色
      bgColor: '#fff', // 导航栏背景颜色
      isloading: true,
      isColor: true,
    },
    name:'',
    uuid: "",
    ischange: false,
    typels: [{
      ischoice: false,
      value: '已备案'
    }, {
      ischoice: false,
      value: '未备案'
    }],
    ischoice: 0,
    typeIndex: 0, //申诉类型选择下标
    appealLs: [], //
    appealTypeLs: [], //申诉类型数组(用于pick选择)
    addressIndex: 0, //区域下标
    addressLs: [], //区域数组
    addressList: [], //区域选择数组(用于pick选择)
    uploaderList: [],
    imglist: [],
    imgHttp: '',
    array: [],
    index: 0,
    areaId: '', //首页记录的areaId
    filter: {
      name: '', //姓名
      contact: '', //手机号
      merchantType: 1, //商家类型 申诉商家类型 1 已备案商家 2未备案过
      type: '', //申诉类型 gov_appeal_type
      merchantId: '', //商家id
      shopName: '', //店铺名称
      title: '', //标题
      content: '', //正文
      imgs: [], //图片
      code: '',
      uuId: '',
      address: "",
      areaId: '', //区域id，未备案商家填写
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      areaId: wx.getStorageSync('areaId')
    })
    this.getGovAreaList()
    this.getDict()
    this.govMerchantList()
    this.getcaptchaImage()
  },
  setChange() {
    this.setData({
      ischange: !this.data.ischange,
    })
  },
  setChoice(e) {
    console.log(e)
    this.setData({
      ischoice: e.currentTarget.dataset.index
    })
    this.data.filter.merchantType = e.currentTarget.dataset.index + 1
  },
  nameInput(e) {
    this.data.filter.name = e.detail.value
    this.data.name = e.detail.value
  },
  phoneInput(e) {
    this.data.filter.contact = e.detail.value
  },
  shopInput(e) {
    this.data.filter.shopName = e.detail.value
  },
  titleInput(e) {
    this.data.filter.title = e.detail.value
  },
  contentInput(e) {
    this.data.filter.content = e.detail.value
  },
  codeInput(e) {
    this.data.filter.code = e.detail.value
  },
  addressInput: function (e) {
    this.data.filter.address = e.detail.value
  },
  getGovAreaList() { //查询区域列表
    WXAPI.govAreaList('', (res) => {
      let ls = res.result
      let ls2 = ['请选择']
      let list = ls2.concat(ls.map(x => {
        return x.name
      }))
      this.setData({
        addressLs: ls,
        addressList: list
      })
    })
  },
  govMerchantList() { //查询区域列表
    WXAPI.govMerchantList('', (res) => {
      let ls = res.result
      let ls2 = ['请选择']

      let list = ls2.concat(ls.map(x => {
        return x.additionalValue
      }))
      this.setData({
        shopLs: ls,
        array: list
      })
    })
  },
  getDict() { //查询区域列表
    WXAPI.getDict({
      dictType: "gov_appeal_type",
    }, (res) => {
      let ls = res.result[0].dictList
      let ls2 = ['请选择']
      let list = ls2.concat(ls.map(x => {
        return x.value
      }))
      this.setData({
        appealLs: ls,
        appealTypeLs: list
      })
    })
  },
  getcaptchaImage() { //查询区域列表
    WXAPI.getcaptchaImage('', (res) => {
      this.setData({
        codeImg: res.result.img,
      })
      this.data.filter.uuId = res.result.uuid
    })
  },
  bindPickerChangeType(e) { //申诉类型选择
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      typeIndex: e.detail.value
    })
    if (this.data.typeIndex == 0) {
      this.data.filter.type = ''
    } else {
      this.data.filter.type = this.data.appealLs[this.data.typeIndex - 1].id
    }
    // console.log(this.data.filter.type,this.data.appealLs)
  },
  addressChangeType(e) { //管辖区域选择
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      addressIndex: e.detail.value
    })
    if (this.data.addressIndex == 0) {
      this.data.filter.areaId = ''
    } else {
      this.data.filter.areaId = this.data.addressLs[this.data.addressIndex - 1].id
    }
    // console.log(this.data.filter.areaId)
  },
  bindPickerChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
    if (this.data.index == 0) {
      this.data.filter.merchantId = ''
      this.data.filter.shopName = ''
    } else {
      this.data.filter.merchantId = this.data.shopLs[this.data.index - 1].id
      this.data.filter.shopName = this.data.shopLs[this.data.index - 1].additionalValue
    }
    // console.log(this.filter.merchantId)
  },
  chooseImg() {
    var _this = this
    wx.chooseMedia({
      count: 1,
      sourceType: ['album'], //从相册选择
      success: (res) => {
        wx.showLoading({
          title: '上传中'
        });
        console.log(JSON.stringify(res.tempFiles));
        console.log('res.tempFilePaths', res.tempFiles)
        for (var i = 0; i < res.tempFiles.length; i++) {
          var data = {
            file: res.tempFiles[i].tempFilePath
          }
          WXAPI.appUploadFile(data, (res) => {
            console.log("上传图片", res.result.url)
            let imgdata = res
            if (imgdata.code != 200) {
              wx.showToast({
                icon: 'none',
                title: imgdata.message
              })
            } else {
              let imgurl = imgdata.result.url
              let list = _this.data.uploaderList
              list.push(imgurl)
              _this.data.filter.imgs = list
              _this.setData({
                uploaderList: list,
                imglist: list
              })
            }
            wx.hideLoading()
          })
        }
      }
    })
  },
  lookImg (e) {
    console.log(e)
    const urls = this.data.imglist
    const current = e.currentTarget.dataset.item
    wx.previewImage({
      urls,
      current
    })
  },
  cancelImg(e) {
    var index = e.currentTarget.dataset.index
    var list = this.data.uploaderList
    list.splice(index, 1);
    this.setData({
      uploaderList: list,
      imglist: list,
    })
  },
  //提交
  sumit() {
    if (this.data.ischange == false && this.data.name == '') {
      wx.showToast({
        icon: 'none',
        title: '申诉人不能为空'
      })
      return
    }
    if (this.data.filter.merchantType == 2 && this.data.filter.areaId == '') {
      wx.showToast({
        icon: 'none',
        title: '请选择管辖区域'
      })
      return
    }
    if (this.data.ischange == true) {
      this.data.filter.name = ''
    } else {
      console.log(this.data.filter.name)
      this.data.filter.name = this.data.name
    }
    console.log(this.data.filter)
    WXAPI.addAppeal({
      ...this.data.filter
    }, (res) => {
      if (res.code == 200) {
        wx.showToast({
          icon: 'success',
          title: '提交成功'
        })
        setTimeout(() => {
          wx.redirectTo({
            url: '../superviseIndex/superviseIndex'
          });
        }, 1000)
      }
      if (res.message == '验证码已失效') {
        this.getcaptchaImage()
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})